﻿#include "usertradewidget.h"
#include "ui_usertradewidget.h"
#include <QPainter>
#include "qrencode.h"
#include <QDebug>

UserTradeWidget::UserTradeWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UserTradeWidget)
{
    ui->setupUi(this);

    setWindowTitle("积分充值");

    day = 0;
    totalAmount = "0";
    connect(&tradeService, &TradeService::getQrCodeSuccess,
            [=](QString qrCode)
    {
        ui->labelQrCode->setPixmap(createQrCode(qrCode));
    });

    connect(&tradeService, &TradeService::queryUserInfoSuccess,
            [=](User &user, QString companyName, QString serverDate, bool isValid)
    {
        ui->lineEditUsername->setText(user.username);
        ui->lineEditNickName->setText(user.nickName);
        ui->lineEditCompanyName->setText(companyName);
        ui->labelEndDate->setText("到期时间："+user.endDate);
        ui->labelServerDate->setText("服务器时间："+serverDate);
        if(isValid){
            //设置颜色
            QPalette pa;
            pa.setColor(QPalette::WindowText,Qt::green);
            ui->labelEndDate->setPalette(pa);
        }else{
            //设置颜色
            QPalette pa;
            pa.setColor(QPalette::WindowText,Qt::red);
            ui->labelEndDate->setPalette(pa);
        }
    });
}

UserTradeWidget::~UserTradeWidget()
{
    delete ui;
}

QPixmap UserTradeWidget::createQrCode(const QString &text){
    int margin = 2;
    if (text.length() == 0)
    {
        return QPixmap();
    }
    QRcode *qrcode = QRcode_encodeString(text.toLocal8Bit(), 1, QR_ECLEVEL_L, QR_MODE_8, 1);
    if (qrcode == nullptr) {
        return QPixmap();
    }
    unsigned char *p, *q;
    p = nullptr;
    q = nullptr;
    int x, y, bit;
    int realwidth;

    realwidth = qrcode->width;
    QImage image = QImage(realwidth, realwidth, QImage::Format_Indexed8);
    QRgb value;
    value = qRgb(255, 255, 255);
    image.setColor(0, value);
    value = qRgb(0, 0, 0);
    image.setColor(1, value);
    image.setColor(2, value);
    image.fill(0);
    p = qrcode->data;
    for (y = 0; y<qrcode->width; y++) {
        bit = 7;
        q += margin / 8;
        bit = 7 - (margin % 8);
        for (x = 0; x<qrcode->width; x++) {
            if ((*p & 1) << bit)
                image.setPixel(x, y, 1);
            else
                image.setPixel(x, y, 0);
            bit--;
            if (bit < 0)
            {
                q++;
                bit = 7;
            }
            p++;
        }
    }

    return QPixmap::fromImage(image.scaledToWidth(ui->labelQrCode->width()));
}

void UserTradeWidget::on_pushButtonQrCode_clicked()
{
    tradeService.getQrCode(this, ui->lineEditUsernameQuery->text(), day, totalAmount);
}

void UserTradeWidget::on_comboBoxTradeTime_currentIndexChanged(int index)
{
    switch (index) {
    case 0:
        day = 0;
        break;
    case 1:
        day = 30;
        break;
    case 2:
        day = 60;
        break;
    case 3:
        day = 90;
        break;
    case 4:
        day = 180;
        break;
    case 5:
        day = 360;
        break;
    case 6:
        day = 720;
        break;
    }

    double money = day*0.01;
    totalAmount = QString("%1").arg(money);
    ui->lineEditTradeMoney->setText(QString("%1").arg(totalAmount));
}

void UserTradeWidget::on_pushButtonQuery_clicked()
{
    tradeService.queryUserInfo(this, ui->lineEditUsernameQuery->text());
}
